Homework

library(CodeClanData)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
qb_revenue_breakdown
#head(qb_revenue_breakdown)

qb_monthly_sales
#head(qb_monthly_sales)

qb_competitors
#head(qb_competitors)

qb_device_data
#head(qb_device_data)

2.1 Part 1

Question 1.

Take the data in the dataset qb_revenue_breakdown and make a stacked bar chart showing the sources of revenue across the two years in the dataset.

ggplot(qb_revenue_breakdown) +
  geom_bar(aes(x = Product, y = Revenue, fill = Year), stat = "identity")

Question 2

Make a line chart showing monthly sales of the “flagship product” - the ‘KwikBit Unit 1’ - over the last year. This data is inside qb_monthly_sales, but you will need to use filter() or subset() (base R).

#qb_monthly_sales

last_year_qb_monthly_sales <- qb_monthly_sales %>% 
  filter(Cashflow == "Kwikbit Unit 1 Sales") 
  
#last_year_qb_monthly_sales

ggplot(last_year_qb_monthly_sales) +
geom_line(
    aes(x = Date, y = Amount))

Question 3.

Make a line chart showing monthly revenue and costs over the last year. This data is also in qb_monthly_sales.

qb_monthly_sales
last_year_qb_monthly_revenue_costs <- qb_monthly_sales %>% 
  filter(Cashflow != "Kwikbit Unit 1 Sales") 

  
last_year_qb_monthly_revenue_costs
ggplot(last_year_qb_monthly_revenue_costs) +
geom_line(
    aes(x = Date, y = Amount, group = Cashflow, colour = Cashflow))

Question 4.

Show annual sales of personal fitness trackers over the last 5 years broken down by company as a ribbon plot (use geom_area). This data is in qb_competitors.

qb_competitors
#?geom_area
ggplot(qb_competitors) +
  aes(x = Year, y = Revenue, fill = Company) +
  geom_area()+
  scale_y_continuous(labels = scales::comma)

Question 5.

Now show the sales from the four competitors as a line graph. Include an extra layer that shows the data points used to make the lines.

ggplot(qb_competitors) +
  geom_line(aes(x = Year, y = Revenue, colour = Company)) +
  geom_point(aes(x = Year, y = Revenue, colour = Company))

Question 6.

Now the company wants to compare the number of steps that their device counts vs. their competitors. Make a line graph of the number of steps throughout time, and use faceting to compare between companies and people. The data for this is in qb_device_data.

qb_device_data #%>% 
#str(qb_device_data)

qb_device_data <- qb_device_data %>% 
  mutate(time = hours + (mins/60))

#head(qb_device_data)

ggplot(qb_device_data) +
  geom_line(aes(x = time, y = counts, colour = device)) +
  facet_wrap(~ device)

ggplot(qb_device_data) +
  geom_line(aes(x = time, y = counts, colour = id)) +
  facet_wrap(~ id)

2.2 Part 2

Question 1.

Take the data in the dataset qb_revenue_breakdown and make a stacked bar chart showing the sources of revenue across the two years in the dataset.

head(qb_revenue_breakdown)
col_scheme <- c("#E89FE9", "#50434F", "#B6A7B5", "#F9A472", "#BD7040")


ggplot(qb_revenue_breakdown) +
  geom_bar(aes(x = Product, y = Revenue, fill = Year), stat = "identity")+
  labs(
    x = "\nProduct",
    y = "Revenue (£)\n",
    title = "Sources of revenue",
     subtitle = "(2018-19)",
    fill = "Year"
  )+
  scale_fill_manual(values = col_scheme)+
  
 # ?theme_light
  theme_light(12)+
    theme(axis.text = element_text(size = 10))

Make a line chart showing monthly sales of the “flagship product” - the ‘KwikBit Unit 1’ - over the last year. This data is inside qb_monthly_sales, but you will need to use filter() or subset() (base R).

col_scheme <- c("#E89FE9", "#50434F", "#B6A7B5", "#F9A472", "#BD7040")

head(last_year_qb_monthly_sales)
ggplot(last_year_qb_monthly_sales) +
geom_line(
    aes(x = Date, y = Amount), colour = col_scheme[1])+
    theme_light(12)+
    theme(axis.text = element_text(size = 10))+
  labs(
    x = "\nMonth",
    y = "Amount (£)\n",
    title = "Monthly sales",
    subtitle = "KwikBit Unit 1",
  ) 

Make a line chart showing monthly revenue and costs over the last year. This data is also in qb_monthly_sales.

col_scheme <- c("#E89FE9", "#50434F", "#B6A7B5", "#F9A472", "#BD7040")

last_year_qb_monthly_revenue_costs <- qb_monthly_sales %>% 
  filter(Cashflow != "Kwikbit Unit 1 Sales") 

last_year_qb_monthly_revenue_costs
ggplot(last_year_qb_monthly_revenue_costs) +
    theme_light(12)+
    theme(axis.text = element_text(size = 10))+
geom_line(
    aes(x = Date, y = Amount, group = Cashflow, colour = Cashflow))+
      scale_color_manual(values=col_scheme)+

    labs(
    x = "\nMonth",
    y = "Amount (£)\n",
    title = "Monthly revenue and costs",
  ) 

Show annual sales of personal fitness trackers over the last 5 years broken down by company as a ribbon plot (use geom_area). This data is in qb_competitors.

col_scheme <- c("#E89FE9", "#50434F", "#B6A7B5", "#F9A472", "#BD7040")

#?geom_area
ggplot(qb_competitors) +
  aes(x = Year, y = Revenue, fill = Company) +
  geom_area()+
   theme_light(12)+
    theme(axis.text = element_text(size = 10))+
  scale_y_continuous(labels = scales::comma)+
  scale_fill_manual(values=col_scheme)+
    labs(
    x = "\nYear",
    y = "Amount (£)\n",
    title = "Show annual sales of personal fitness trackers",
    subtitle = "2015-19"
  )

ggplot(qb_competitors) +
  geom_line(aes(x = Year, y = Revenue, colour = Company)) +
  geom_point(aes(x = Year, y = Revenue, colour = Company))+
    scale_colour_manual(values=col_scheme)+
    theme_light(12)+
    theme(axis.text = element_text(size = 12))+
 
    labs(
    x = "\nYear",
    y = "Amount (£)\n",
    title = "Annual sales of personal fitness trackers",
    subtitle = "2015-19"
  )

Now the company wants to compare the number of steps that their device counts vs. their competitors. Make a line graph of the number of steps throughout time, and use faceting to compare between companies and people. The data for this is in qb_device_data.

ggplot(qb_device_data) +
  geom_line(aes(x = time, y = counts, colour = device)) +
  facet_wrap(~ device)+
   scale_colour_manual(values=col_scheme)+
    theme_light(12)+
    theme(axis.text = element_text(size = 12))+
 labs(
    x = "\nTime",
    y = "Steps\n",
    title = "Number of steps vs competitors",
  )

ggplot(qb_device_data) +
  geom_line(aes(x = time, y = counts, colour = id)) +
  facet_wrap(~ id)+

   scale_colour_manual(values=col_scheme)+
    theme_light(12)+
    theme(axis.text = element_text(size = 12))+
 labs(
    x = "\nTime",
    y = "Steps\n",
    title = "Number of steps by person",
  )